
/**
 * 用途         :常用UI,字符串,数组通用方法,基本上自制方法都在这里
 * 开发者       :郑品延
 * 修改时间     :2019/8/6
 */
export default class JayUtil{
    public static platform = '';
    public static setPlatform() { //暂时只做简单判断，wx/other
        if (cc.sys.platform == cc.sys.MACOS) {
            this.platform = "macOs";
        }
        else if (cc.sys.platform == cc.sys.ANDROID) {
            this.platform = "android";
        }
        else if (cc.sys.platform == cc.sys.IPHONE) {
            this.platform = "iphone";
        }
        else if (cc.sys.platform == cc.sys.IPAD) {
            this.platform = "ipad";
        }
        else if (cc.sys.platform == cc.sys.MOBILE_BROWSER) {
            this.platform = "mobileBrowser";//手机流浪器
        }
        else if (cc.sys.platform == cc.sys.DESKTOP_BROWSER) {
            this.platform = "desktopBrowser";//桌面流浪器
        }
        else if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            this.platform = "weChat";
        }
        else if (cc.sys.platform == cc.sys.QQ_PLAY) {
            this.platform = "qqPlay";
        } 
        else if (cc.sys.platform == cc.sys.BAIDU_GAME) {
            this.platform = "baiduGame";
        } 
        else if (cc.sys.platform == cc.sys.OPPO_GAME) {
            this.platform = "oppoGame";
        }
        else if (cc.sys.platform == cc.sys.VIVO_GAME) {
            this.platform = "vivoGame";
        }
        else {
            this.platform = "macOs";
        }
    }
    /**
     * 获取当前平台名称
     */
    public static getPlatform() {
        return this.platform;
    }
    /**
     * 节点转到目标节点坐标系,返回坐标
     * @param srcNode 
     * @param destNode 
     */
    public static convertToTargetNodeSpace (srcNode : cc.Node, destNode:cc.Node) {
        let worldPos = srcNode.convertToWorldSpaceAR(cc.v3(0, 0, 0));
        let nodePost = destNode.convertToNodeSpaceAR(worldPos);
        return nodePost;
    }
    /**
     * 是否是中文
     */
    public static isChinese () {
        let currentLanguageType = cc.sys.language;
        if (currentLanguageType == cc.sys.LANGUAGE_CHINESE) {
            return true;
        }
        return false;
    }

    /**
     * 拆分字符串
     * @param str 
     * @param fuhao 
     */
    public static splitStringToNumberArray(str, fuhao) {
        if (!str){
            return [];
        }
        let array = str.split(fuhao);
        let newArray = [];
        for (let i = 0; i < array.length; i++) {
            newArray.push(array[i]);
        }
        return newArray;
    }
    /**
     * 获取对象一共有多少个属性
     * @param obj 
     */
    public static getObjectLength(obj) {
        return Object.keys(obj).length;
    }
    /**
     * 宽屏进行节点坐标的缩放
     * @param node 
     */
    public static scalePosition(node) {
        let winWidth = cc.winSize.width;
        let designWidth = 750;  //设计分辨率
        if (winWidth > designWidth) {
            let scale = winWidth / designWidth;
            node.x = node.x * scale;
        }
    }
    /**
     * 数字转汉字
     * @param num 
     */
    public static NumberToChinese(num) {
        let unitPos = 0;
        let strIns = '', chnStr = '';
        let needZero = false;

        let chnNumChar = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
        let chnUnitSection = ["", "万", "亿", "万亿", "亿亿"];
        let chnUnitChar = ["", "十", "百", "千"];
        function SectionToChinese(section) {
            let strIns = '', chnStr = '';
            let unitPos = 0;
            let zero = true;
            while (section > 0) {
                let v = section % 10;
                if (v === 0) {
                    if (!zero) {
                        zero = true;
                        chnStr = chnNumChar[v] + chnStr;
                    }
                } else {
                    zero = false;
                    strIns = chnNumChar[v];
                    strIns += chnUnitChar[unitPos];
                    chnStr = strIns + chnStr;
                }
                unitPos++;
                section = Math.floor(section / 10);
            }
            return chnStr;
        }

        if (num === 0) {
            return chnNumChar[0];
        }

        while (num > 0) {
            let section = num % 10000;
            if (needZero) {
                chnStr = chnNumChar[0] + chnStr;
            }
            strIns = SectionToChinese(section);
            strIns += (section !== 0) ? chnUnitSection[unitPos] : chnUnitSection[0];
            chnStr = strIns + chnStr;
            needZero = (section < 1000) && (section > 0);
            num = Math.floor(num / 10000);
            unitPos++;
        }

        return chnStr;
    }
    /**
     * 将数组随机 ，取出前count个值，返回一个新的数组
     * @param lottArray 
     * @param count 
     */
    public static lotteryArray(lottArray :Array<string>|string,count:number){
        if (count >= lottArray.length){
            return [lottArray[0]];
        }
        let array = null;
        if(typeof(lottArray)=='string'){
            array = lottArray.split('');
        }
        else{
            array = lottArray;
        }
        let result = [];
        for (let i = 0; i < count; i++) {
            let ran = Math.floor(Math.random() * (array.length - i));
            result.push(array[ran]);
            array[ran] = array[array.length - i - 1];
        };
        return result;
    }
    public static showLayer(node) {
        node.active = true;
        let rootNode = node.getChildByName('RootNode');
        if (rootNode) {
            rootNode.scale = 0;
            rootNode.runAction(cc.scaleTo(0.3, 1));
            // rootNode.opacity = 0;
            // rootNode.runAction(cc.fadeIn(0.5))
        }
    }
    public static closeLayer(node) {
        let rootNode = node.getChildByName('RootNode');
        if (rootNode) {
            let seq = cc.sequence(cc.scaleTo(0.1, 1.1), cc.scaleTo(0.3, 0), cc.callFunc(() => { node.destroy() }))
            rootNode.runAction(seq);
        }
        else {
            node.destroy();
        }
    }
    /**
     * 打乱数组，返回新数组，原数组不动
     * @param b 
     */
    public static shuffle(b) {
        let a = [].concat(b);
        let len = a.length;
        for (let i = 0; i < len; i++) {
            let end = len - 1;
            let index = (Math.random() * (end + 1)) >> 0;
            let t = a[end];
            a[end] = a[index];
            a[index] = t;
        }
        return a;
    }
    /**
     * 获取当前ScrollView滚动的百分比,适用于Scroll里的View节点为锚点0.5,conent方向锚点1
     * @param view 
     * @param isVert 是否垂直方向
     */
    public static getScrollPercent(view:cc.ScrollView,isVert=true){
        let lookLen = isVert ? view.node.height : view.node.width;
        let contentLen = isVert ? view.content.height : view.content.width;
        if(contentLen<=lookLen){
            return 0;
        }
        let defaultPos = isVert ? view.node.height/2 : view.node.width/2;
        let moveLen = Math.abs(isVert ? view.content.y - defaultPos : view.content.x - defaultPos);
        let moveMax = contentLen - lookLen;
        let percent = moveLen / moveMax;
        if (percent > 1) percent = 1;
        if (percent < 0) percent = 0;
        return percent;
    }
    
}
